package com.jeecms.wicool.manager.user.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeecms.cms.entity.main.CmsUser;
import com.jeecms.common.hibernate3.Updater;
import com.jeecms.common.hibernate3.Updater.UpdateMode;
import com.jeecms.common.page.Pagination;
import com.jeecms.common.security.encoder.Md5PwdEncoder;
import com.jeecms.core.Constants;
import com.jeecms.wicool.dao.user.AddressDao;
import com.jeecms.wicool.dao.user.CheckCodeDao;
import com.jeecms.wicool.dao.user.UserInfoDao;
import com.jeecms.wicool.entity.shops.ShopInfo;
import com.jeecms.wicool.entity.user.Address;
import com.jeecms.wicool.entity.user.CheckCode;
import com.jeecms.wicool.entity.user.UserInfo;
import com.jeecms.wicool.manager.car.CarAdviserMng;
import com.jeecms.wicool.manager.shops.WiShopInfoMng;
import com.jeecms.wicool.manager.user.UserInfoMng;

@Service
@Transactional
public class UserInfoMngImpl implements UserInfoMng {
	
	private UserInfoDao dao;
	@Autowired
	public void setDao(UserInfoDao dao) {
		this.dao = dao;
	}
	
	@Transactional(readOnly = true)
	public Pagination getPage(Integer shopId,String mobile,String name,Short status,Integer carAdviserId,
			Date q_createTime_start,Date q_createTime_end,int pageNo, int pageSize,CmsUser user) {
		Pagination page = dao.getPage(shopId,mobile, name, status, carAdviserId,q_createTime_start,
				q_createTime_end,pageNo, pageSize,user);
		return page;
	}
	@Transactional(readOnly = true)
	public Pagination getPageAddress(String userName,Integer shopId,String consigneeName,int pageNo, int pageSize) {
		Pagination page = addressDao.getPage(shopId, userName, consigneeName, pageNo, pageSize);
		return page;
	}
	@Override
	public UserInfo findById(Integer id) {
		UserInfo entity = dao.findById(id);
		return entity;
	}
	@Override
	public Address findByIdAddress(Integer id) {
		Address entity = addressDao.findById(id);
		return entity;
	}
	@Override
	public UserInfo update(UserInfo bean,Integer shopId,Integer carAdviserId) {
		
		ShopInfo shop = shopMng.findById(shopId);
		bean.setShop(shop);
		if(carAdviserId != null){
			UserInfo carAdviser = dao.findById(carAdviserId);
			bean.setSceneUser(carAdviser);
		}else {
			bean.setSceneUser(null);
		}
		Updater<UserInfo> updater = new Updater<UserInfo>(bean,UpdateMode.MAX);
		bean = dao.updateByUpdater(updater);
		return bean;
	}
	@Override
	public Address updateAddress(Address bean,Integer shopId) {
		
		ShopInfo shop = shopMng.findById(shopId);
		bean.setShop(shop);
		Updater<Address> updater = new Updater<Address>(bean);
		bean = addressDao.updateByUpdater(updater);
		return bean;
	}
	@Override
	public List<Address> updateAddressList(List<Address> listNow, Integer userId) {
		// TODO Auto-generated method stub
		//*****先删除******
		//1.获取该用户下的所有address实体
		List<Address> listAll= addressDao.getListAddressByUserId(userId); // 原来的所有address
		List<Integer> listAllId= new ArrayList<Integer>(); //有所有的sku的skuCode
		List<Integer> listNowId = new ArrayList<Integer>();//当前sku的skuCode
		for(Address all:listAll){
			listAllId.add(all.getId());
		}
		for(Address now:listNow){
			if(now.getId()==null){
				listNowId.add(0);
				continue;
			}
			listNowId.add(now.getId());
		}
		List<Integer> listDelId= new ArrayList<Integer>();
		//2.循环所有sku集合与当前的list进行比较，不存在的说明需要被删除
		for(Integer all:listAllId){
				if(!listNowId.contains(all)){
					listDelId.add(all);
			}
		}
		for(Integer i : listDelId){
			listAll.remove(addressDao.findById(i));
			//删除被remove的实例
			addressDao.deleteById(i);
		}
		
		//******再更新或者添加******
		Updater<Address> updater;
		List<Address> listAdd= new ArrayList<Address>();
			for(Address now:listNow){
				if(now.getId()!=null){
					//更新
					updater = new Updater<Address>(now);
					addressDao.updateByUpdater(updater);
				}else{
					//新增
					listAdd.add(now);
				}
			}
			
		for(Address p:listAdd){
			p.setUser(this.findById(userId));
			p.setShop(this.findById(userId).getShop());
			addressDao.save(p);
		}
		listAll.addAll(listAdd);
		return listAll;
	}
	@Override
	public UserInfo save(UserInfo bean,Integer shopId,Integer carAdviserId) {
		
		ShopInfo shop = shopMng.findById(shopId);
		bean.setShop(shop);
		if(carAdviserId != null){
			UserInfo carAdviser = dao.findById(carAdviserId);
			bean.setSceneUser(carAdviser);
		}else {
			bean.setSceneUser(null);
		}
		dao.save(bean);
		return bean;
	}

	@Override
	public UserInfo deleteById(Integer id) {
		UserInfo bean = dao.deleteById(id);
		return bean;
	}
	@Override
	public UserInfo[] deleteByIds(Integer[] ids) {
		UserInfo[] beans = new UserInfo[ids.length];
		for (int i = 0,len = ids.length; i < len; i++) {
			beans[i] = deleteById(ids[i]);
		}
		return beans;
	}
	@Override
	public Address deleteByIdAddress(Integer id) {
		Address bean = addressDao.deleteById(id);
		return bean;
	}
	@Override
	public Address[] deleteByIdsAddress(Integer[] ids) {
		Address[] beans = new Address[ids.length];
		for (int i = 0,len = ids.length; i < len; i++) {
			beans[i] = deleteByIdAddress(ids[i]);
		}
		return beans;
	}
	@Override
	public List<Address> getListAddressByUserId(Integer userId) {
		return addressDao.getListAddressByUserId(userId);
	}
	
	@Override
	public UserInfo resetPwd(Integer id) {
		UserInfo bean = findById(id);
		if(bean == null){
			return null;
		}
		ShopInfo shop = shopMng.findById(bean.getShop().getId());
		bean.setShop(shop);
		UserInfo carAdviser = dao.findById(bean.getSceneUser().getId());
		bean.setSceneUser(carAdviser);
		bean.setPassword(new Md5PwdEncoder().encodePassword(Constants.DEFAULT_USER_PASSWORD));
		Updater<UserInfo> updater = new Updater<UserInfo>(bean,UpdateMode.MAX);
		bean = dao.updateByUpdater(updater);
		return bean;
	}
	
	@Autowired
	private WiShopInfoMng shopMng;
	@Autowired
	private AddressDao addressDao;
	@Autowired
	private CarAdviserMng carAdviserMng;
	
	@Autowired
	private CheckCodeDao checkCodeDao;
	
	@Override
	public List<UserInfo> getAll(CmsUser user) {
		return dao.getAll(user);
	}

	public Pagination getPageCheckCode(String school,String classes,String checkcode,int pageNo, int pageSize){
		return checkCodeDao.getPage(school, classes, checkcode, pageNo, pageSize);
	}
	
	public CheckCode getCheckCodeById(Integer id){
		return checkCodeDao.findById(id);
	}
	
	public CheckCode saveCheckCode(CheckCode bean){
		return checkCodeDao.save(bean);
	}
	
	public CheckCode updateCheckCode(CheckCode bean){
		Updater<CheckCode> updater = new Updater<CheckCode>(bean);
		return checkCodeDao.updateByUpdater(updater);
	}
	
	public CheckCode deleteCheckCode(Integer id){
		return checkCodeDao.deleteById(id);
	}
}